package com.program.facesigninsystem.controller;

import com.program.facesigninsystem.entity.Classes;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.service.IClassesService;
import com.program.facesigninsystem.service.IUserClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;
import java.util.Random;

@CrossOrigin
@RestController
@RequestMapping(value = "/class")
public class ClassesController {
    @Autowired
    @Qualifier("Classes")
    private IClassesService iClassesService;
    @RequestMapping(value = "/selectClassById",method = RequestMethod.GET)
    private ResponseModel selectClassById(@RequestParam("cid")String cid){
        ResponseModel responseModel = new ResponseModel(200,"查询成功",iClassesService.selectClasses(cid));
        return responseModel;
    }

    @RequestMapping(value = "/createClass",method = RequestMethod.GET)
    private ResponseModel createNewClass(@RequestParam("tid")String tid,@RequestParam("name")String name){
        String cid = "";
        int count=0;
        Random random = new Random();
        for (int i = 0; i < 20; i++) {
            cid += String.valueOf(random.nextInt(10));
        }
        ResponseModel responseModel;
        try {
            iClassesService.insertClasses(cid,name,count,tid);
            responseModel = new ResponseModel(200,"新建成功");
        }catch (Exception e){
            responseModel = new ResponseModel(199,"新建失败");
        }
        return responseModel;
    }

    @Autowired
    @Qualifier("UserClass")
    private IUserClassService iUserClassService;
    @RequestMapping(value = "/joinClass",method = RequestMethod.GET)
    private ResponseModel joinClass(@RequestParam("uid")String uid,@RequestParam("cid")String cid){
        int count=iUserClassService.selectUserClass(uid,cid);
        ResponseModel responseModel;
        if(count==0){
            iUserClassService.insertUserClass(uid,cid);
            iClassesService.updateClasses(cid);
            responseModel = new ResponseModel(200,"加入成功");
        }else{
            responseModel = new ResponseModel(199,"您已加入该名单");
        }
        return responseModel;
    }

    @RequestMapping(value = "/findClassListByTid",method = RequestMethod.GET)
    private  ResponseModel findClassListByTid(@RequestParam("tid")String tid){
        ResponseModel responseModel;
        List<Classes> classesList = iClassesService.selectClassListByTid(tid);
        responseModel = new ResponseModel(200,"查询成功",classesList);
        return responseModel;
    }
}
